#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

int max(int a,int b);
int *buildST(int *,int,int *);
int buildSegmentTree(int *,int *,int,int,int);
int findLeftMost(int *sT,int pos,int left,int right,int val);



int max(int a,int b){
    return a>b?a:b;
}
int *buildST(int * arr,int size,int *sTSize){
    int sz=4*size;
    *sTSize=sz;
    int *st=malloc(sizeof(int)*sz);
    buildSegmentTree(arr,st,1,0,size-1);
    return st;
}
int buildSegmentTree(int *arr,int *seg,int pos,int left,int right){
    if( left == right ){
        seg[pos]=arr[left];
        return seg[pos];
    }
    int mid=left+((right-left)>>1);
    int tmp1=buildSegmentTree(arr,seg,2*pos,left,mid);
    int tmp2=buildSegmentTree(arr,seg,2*pos+1,mid+1,right);
    seg[pos]=max(tmp1,tmp2);
    return seg[pos];
}
int findLeftMost(int *sT,int pos,int left,int right,int val){
    if( sT[pos] < val )
        return -1;
    if( left == right ){
        sT[pos]=0;
    }else{
        int mid=left+((right-left)>>1);
        int tmp1=findLeftMost(sT,2*pos,left,mid,val);
        int tmp2;
        if( -1 == tmp1 ){
            tmp2=findLeftMost(sT,2*pos+1,mid+1,right,val);
            sT[pos]=max(tmp2,sT[2*pos]);
        }
        else{
            sT[pos]=max(tmp1,sT[2*pos+1]);
        }
    }
    return sT[pos];
}
int numOfUnplacedFruits(int* fruits, int fruitsSize, int* baskets,int basketsSize){
    int sTSize;
    int *sT=buildST(baskets,basketsSize,&sTSize);
    int r=0;
    for(int i=0;i<fruitsSize;i++) {
        /*
        for(int i=0;i<sTSize;i++)
            printf("%d ",sT[i]);
        printf("\n");
        */
        if( findLeftMost(sT,1,0,fruitsSize-1,fruits[i]) == -1 ){
            //printf("%d \n",fruits[i]);
            r++;
        }
    }
    return r;
}
int main(){
    int fruits[]={3,6,1};
    int baskets[]={6,4,7};
    
    printf("%d\n",numOfUnplacedFruits(fruits,sizeof(fruits)/sizeof(int),\
                    baskets,sizeof(baskets)/sizeof(int)));
    return 0;
}
